using System;
using System.ComponentModel;
using System.Management;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class RecycleAppPoolStep : ActionStep
    {
        public string m_appPoolName = StepConstants.NAME;

        [DisplayName( "Application Pool" )]
        [Description( "The name of the application pool to recycle on the server." )]
        public string AppPoolName
        {
            get
            {
                return m_appPoolName;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Application Pool cannot be empty." );
                }
                else
                {
                    m_appPoolName = value.Trim();
                }
            }
        }

        public override string Name
        {
            get { return "Recycle App Pool"; }
        }

        protected override void Run()
        {
            ManagementObject obj = Common.GetWmiObject( @"\\{0}\root\MicrosoftIISv2", CurrentServer,
                "IISApplicationPool", string.Format( "W3SVC/AppPools/{0}", m_appPoolName ) );

            obj.InvokeMethod( "Recycle", null );
        }

        protected override void Simulate()
        {
            ManagementObject obj = Common.GetWmiObject( @"\\{0}\root\MicrosoftIISv2", CurrentServer,
                "IISApplicationPool", string.Format( "W3SVC/AppPools/{0}", m_appPoolName ) );

            Progress.AddFormat( "Simulated app pool recycle of {0} on {1}", m_appPoolName, CurrentServer );
        }
    }
}
